package gold.digger;

import gold.vo.TreeNode;

import java.util.*;
import java.util.Queue;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC662 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
     * @param 此题目参考了别人代码
     * 这是因为问题情况较为复杂
     * 未来需要再次复习此道题目
     * 不得不说解法很妙，位置编号解法我一直在想如何做，但一直没想到合适的pos变更方程
     * 这里面pos * 2 和 pos * 2 + 1 确实是一个关键
     * 想到这一点，整个题就很简单了
     * @return:
     */
    class Solution {
        public int widthOfBinaryTree(TreeNode root) {
            Queue<AnnotatedNode> queue = new LinkedList();
            queue.add(new AnnotatedNode(root, 0, 0));
            int curDepth = 0, left = 0, ans = 0;
            while (!queue.isEmpty()) {
                AnnotatedNode a = queue.poll();
                if (a.node != null) {
                    queue.add(new AnnotatedNode(a.node.left, a.depth + 1, a.pos * 2));
                    queue.add(new AnnotatedNode(a.node.right, a.depth + 1, a.pos * 2 + 1));
                    if (curDepth != a.depth) {
                        curDepth = a.depth;
                        left = a.pos;
                    }
                    ans = Math.max(ans, a.pos - left + 1);
                }
            }
            return ans;
        }
    }

    class AnnotatedNode {
        TreeNode node;
        int depth, pos;

        AnnotatedNode(TreeNode n, int d, int p) {
            node = n;
            depth = d;
            pos = p;
        }
    }

    class Solution_Time_Exceed {
        public int widthOfBinaryTree(TreeNode root) {
            if (null == root) return 0;
            Deque<TreeNode> queue = new LinkedList<>();
            queue.offerFirst(root);

            TreeNode dummy = new TreeNode(-1);
            int maxWidth = 0;
            while (!queue.isEmpty()) {
                while (queue.peek() == dummy) queue.pollFirst();
                while (queue.peekLast() == dummy) queue.pollLast();

                int size = queue.size();
                maxWidth = Math.max(maxWidth, size);
                for (int i = 0; i < size; i++) {
                    TreeNode cur = queue.poll();
                    if (dummy == cur) {
                        queue.offer(dummy);
                        queue.offer(dummy);
                        continue;
                    }

                    if (null == cur.left) queue.offer(dummy);
                    else queue.offer(cur.left);

                    if (null == cur.right) queue.offer(dummy);
                    else queue.offer(cur.right);
                }
            }

            return maxWidth;
        }
    }

    public void run() {
        Solution solution = new Solution();
//        TreeNode root = TreeNode.createTree("[1,3,2,5,3,null,9]");
//        TreeNode root = TreeNode.createTree("[1,3,null,5,5]");
//        TreeNode root = TreeNode.createTree("[1,3,2,5]");
        TreeNode root = TreeNode.createTree("[1,1,1,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,null,1,1,null,1,null,1,null,1,null,1,null]");
        System.out.println(solution.widthOfBinaryTree(root));
    }

    public static void main(String[] args) throws Exception {
        LC662 an = new LC662();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
